//
//  SchoolCommentSubmitPicView.swift
//  HoneyBee
//
//  Created by point on 2018/6/10.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit

protocol SchoolCommentSubmitPicViewDelegate : class {
    func didSelectItemAt(_ indexPath:IndexPath)
}


private let cellID = "cellID"

class SchoolCommentSubmitPicView: UIView {
    
    weak var delegate : SchoolCommentSubmitPicViewDelegate?
    var imgArr : [UIImage] = [UIImage]()

    fileprivate lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 100, height: 100)
        layout.minimumLineSpacing = 10 //行间距
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(SchoolCommentPicCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        collectionView.isPagingEnabled = true
        collectionView.bounces = true
        collectionView.scrollsToTop = false
        collectionView.showsHorizontalScrollIndicator = false
        
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        if imgArr.count == 0 {
            imgArr.append(UIImage(named: "icon_48")!)
        }
        self.collectionView.reloadData()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension SchoolCommentSubmitPicView :SchoolCommentPicCollectionViewCellDelegate{
    func schoolCommentPicCollectionViewCellDelClick(btn: UIButton) {
        imgArr.remove(at: btn.tag)
        self.collectionView.reloadData()
    }
    
    func configModel(imgs:[UIImage]) {
        if imgs.count != 0 {
                imgArr.remove(at: imgArr.count - 1)
                imgArr.append(contentsOf: imgs)
                let deaultImg = UIImage(named: "icon_48")!
                imgArr.append(deaultImg)
            }
            self.collectionView.reloadData()
        
    }
    
    
}

extension SchoolCommentSubmitPicView :UICollectionViewDelegate , UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgArr.count 
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as!SchoolCommentPicCollectionViewCell
        cell.configModel(modelArr: imgArr, index: indexPath)
        cell.delegate = self
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == imgArr.count - 1 {
            delegate?.didSelectItemAt(indexPath)
        }
    }
    
    
    
    
}

extension SchoolCommentSubmitPicView {
    fileprivate func setupUI(){
        addSubview(collectionView)
        collectionView.backgroundColor = UIColor.white
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
    }
}
